layui.use(['table','layer'],function(){
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;


    /*数据表格的渲染*/

    /**
     * 营销机会列表展示
     */
    var  tableIns = table.render({
        elem: '#saleChanceList', // 表格绑定的ID
        url : ctx + '/goods_category/list', // 访问数据的地址
        cellMinWidth : 95,
        page : true, // 开启分页
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        toolbar: "#toolbarDemo",
        id : "saleChanceListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: "id", title:'商品ID',fixed:"true", width:80},
            {field: 'cateName', title: '分类名', align:'center'},
            {field: 'sort', title: '排序',  align:"center"},
            {field: 'createTime', title: '创建时间', align:'center'},
            {field: 'updateTime', title: '更新时间', align:'center'},
            {title: '操作',  templet:'#saleChanceListBar',fixed:"right",align:"center"}
        ]]
    });


    /*绑定头部工具栏*/
    table.on("toolbar(saleChances)",function (obj){
        //获取选中对象状态
        var checkStatus = table.checkStatus(obj.config.id);

        if(obj.event ==='add'){
            //alert("添加");
            openAddOrUpdateSaleChanceDialog();
        }else if(obj.event ==='refresh'){
            //alert("dels");选中数据对象
            refreshSaleChances();
        }

    });

    function refreshSaleChances() {

        tableIns.reload();
    }

    //定义函数
    function openAddOrUpdateSaleChanceDialog(id){
        var title="<h2>商品分类-添加</h2>";
        var url=ctx+"/goods_category/addOrUpdateDialog";
        //判断是否有Id
        if(id){
            var title="<h2>商品分类-更新</h2>";
            url=url+"?id="+id;
        }
        console.log(url);
        //添加弹出层
        layer.open({
            title:title,
            type: 2,//iframe
            area:["500px","620px"],
            maxmin:true,
            content:url
        });
    }


    /*绑定行内工具栏*/
    table.on("tool(saleChances)",function (obj){
        //获取当前行对象
        var data=obj.data;

        if(obj.event==='edit'){
            //alert("编辑");
            openAddOrUpdateSaleChanceDialog(data.id);
            return ;
        }else if(obj.event ==="del"){ //删除
            console.log(obj.event)

            layer.confirm('真的删除行么', function(index){
                //发送ajax删除数据
                $.post(ctx+"/goods_category/delete",{"id":data.id},function(res){
                    //判断
                    if(res.code == 200){
                        //layer.msg("删除成功了");
                        layer.close(index);
                        //刷新
                        tableIns.reload();
                    }else{
                        //提示一下
                        layer.msg(res.msg,{icon:5 });
                    }
                },"json");
            });
        }
    });
});